package clone;

import java.io.*;

/**
 * @author yangwenqi
 * @decs TODO
 * @date 2025/5/15
 */
public class Person implements Cloneable, Serializable {
    private static final long serialVersionUID = 3227696158975092549L;
    private String name;
    private int age;
    private String gender;

    private Phone phone;
    //重写clone（）方法  Person<Object    public>protected
//    @Override
//    public Person clone() throws CloneNotSupportedException {
//        //浅拷贝：基本数据类型拷贝数值，引用数据类型拷贝地址
////        return (Person)super.clone();
//        //深拷贝
//        Person person = (Person) super.clone();
//        Phone p=(Phone)phone.clone();
//        person.setPhone(p);
//        return person;
//    }
    @Override
    public Person clone() throws CloneNotSupportedException {
        try{
            //序列化
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(this);
            oos.flush();

            //反序列化
            ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
            Person person=(Person) ois.readObject();
            return person;

        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    public Person() {
    }

    public Person(String name, int age, String gender, Phone phone) {
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.phone = phone;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    /**
     * 获取
     * @return gender
     */
    public String getGender() {
        return gender;
    }

    /**
     * 设置
     * @param gender
     */
    public void setGender(String gender) {
        this.gender = gender;
    }

    /**
     * 获取
     * @return phone
     */
    public Phone getPhone() {
        return phone;
    }

    /**
     * 设置
     * @param phone
     */
    public void setPhone(Phone phone) {
        this.phone = phone;
    }

    public String toString() {
        return "Person{name = " + name + ", age = " + age + ", gender = " + gender + ", phone = " + phone + "}";
    }
}
